<template>
  <view>
    <privacy-policy
      v-if="showPrivacy"
      @agree="onAgree"
    />
  </view>
</template>

<script>
import PrivacyPolicy from './PrivacyPolicy'

export default {
  components: {
    PrivacyPolicy
  },
  data() {
    return {
      showPrivacy: false
    }
  },
  computed: {
    isIOS() {
      return this.$store.getters.isIOS
    },
  },
  onLoad() {
    if (this.isIOS) {
      // 检查是否需要显示隐私协议
      let privacy = uni.getStorageSync('PrivacyPolicy')
      if (!privacy) {
        this.showPrivacy = true
      } else {
        this.loadApp()
      }
    } else {
      this.loadApp()
    }
  },
  methods: {
    loadApp() {
      this.loadChecked();
      this.tokenChecked();
    },
    onAgree() {
      uni.setStorageSync('PrivacyPolicy', true)
      this.loadApp()
    },
    tokenChecked() {
      try {
        const tokenStr = uni.getStorageSync('token'); //同步获取
        if (tokenStr) {
          // this.$common.setToken(tokenStr)
        } else {
          // this.$common.clearToken()
        }
        let defaultLocation = {
          id: 1,
          name: '全国'
        }
        uni.setStorage({
          key: 'location',
          data: JSON.stringify(defaultLocation)
        })
      } catch (e) {

      }
    },
    loadChecked() {
      try {
        const launch = uni.getStorageSync('launchFlag'); //同步获取
        console.log(launch);
        if (launch) {
          uni.switchTab({
            url: '/pages/index/index'
          });
        } else {
          uni.setStorage({
            key: 'launchFlag',
            data: false
          });
          uni.redirectTo({
            url: '/pages/index/guide'
          });
        }
      } catch (e) {
        //TODO handle the exception
        uni.setStorage({
          key: 'launchFlag',
          data: false,
          success: () => {
            console.log('error时依旧进行存储！');
          }
        });
        uni.redirectTo({
          url: '/pages/index/guide'
        });
      }
    }
  }
};
</script>
<style></style>
